$(function () {
  // 注册接口
  var layer = layui.layer;
  console.log(1);
  var shouji = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;

  //密码的正则
  var pwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
  // var mobile = $("#username").val();
  // // console.log(mobile);
  // var password = $("#password").val();
  $('#btn-reg').on('click', function () {
    //   获取输入框内容
    // console.log(2);
    if (!shouji.test($('#username').val().trim())) {
      return layer.msg('手机格式不正确');
    }
    if (!pwd.test($('#password').val().trim())) {
      return layer.msg('密码最短6位且必须是英文+数字');
    }
    if ($('#password').val() !== $('#new_paw').val()) {
      return layer.msg('两次密码必须相同');
    }

    axios({
      method: 'post',
      url: 'http://124.223.14.236:8060/home/index/reg',
      data: {
        mobile: $('#username').val().trim(),
        password: $('#password').val().trim(),
      },
    }).then(function (res) {
      console.log(res);
      if (res.data.errno == 1000) {
        return layer.msg('账号已存在');
      }
      layer.msg('注册成功');
      localStorage.setItem('token', res.data.data.token);
      setTimeout(function () {
        $('#jump-login')[0].click();
      }, 500);
    });
  });
  
  homePage();
  // 首页和全局数据
  function homePage() {
    axios({
      method: 'GET',
      url: 'http://124.223.14.236:8060/home/index/index',
    }).then(function (res) {
      // console.log(res);
      // 导航栏
      let strNav = res.data.data.allCate
        .map(item => {
          return `
          <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
              <a href="list.html" id=${item.id}><span class="fa fa-bullhorn iconfont ${item.icon}"></span>${item.catename}</a>
          </li>
          `;
        })
        .join('');
      $('#nav').append(strNav);
    });
  }
  // 导航栏跳转
  function getNavSkip() {
    $('#nav').on('click', 'a', function (e) {
      e.preventDefault();
      window.open('list.html?id=' + $(this).attr('id'));
    });
  }
  getNavSkip();
});
